package com.cwf.auth.controller;

import com.cwf.auth.domain.Permission;
import com.cwf.auth.query.PermissionQuery;
import com.cwf.auth.service.IPermissionService;
import com.cwf.basic.annotation.AutoScanPermission;
import com.cwf.basic.util.AjaxResult;
import com.cwf.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(value = "权限的API", description = "权限相关的CRUD功能")
@AutoScanPermission(name = "权限管理", desc = "权限管理接口")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     *
     * @param permission 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "权限的新增和修改")
    @AutoScanPermission(name = "权限的新增和修改", desc = "权限的新增和修改接口")
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        try {
            if (permission.getId() != null)
                permissionService.update(permission);
            else
                permissionService.insert(permission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据权限ID删除权限")
    @AutoScanPermission(name = "根据权限ID删除权限", desc = "根据权限ID删除权限接口")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            permissionService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据权限ID查询权限信息")
    @AutoScanPermission(name = "根据权限ID查询权限信息", desc = "根据权限ID查询权限信息接口")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Permission permission = permissionService.queryById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有权限信息")
    @AutoScanPermission(name = "查询所有权限信息", desc = "查询所有权限信息接口")
    public AjaxResult queryAll() {

        try {
            List<Permission> list = permissionService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @ApiOperation(value = "分页查询所权限信息")
    @AutoScanPermission(name = "分页查询所权限信息", desc = "分页查询所权限信息接口")
    public AjaxResult queryPage(@RequestBody PermissionQuery query) {
        try {
            PageInfo<Permission> pageList = permissionService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    @PutMapping("/tree")
    @ApiOperation(value = "为角色分配权限，获取权限列表")
    @AutoScanPermission(name = "为角色分配权限，获取权限列表", desc = "为角色分配权限，获取权限列表接口")
    public AjaxResult tree() {
        try {
            // 获取权限列表
            List<Permission> tree = permissionService.tree();
            return AjaxResult.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取权限失败！" + e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除权限信息")
    @AutoScanPermission(name = "批量删除权限信息", desc = "批量删除权限信息接口")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            permissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除数据失败！" + e.getMessage());
        }
    }
}
